package com.fastwire.carriere.service;

import java.util.List;

import javax.jws.WebService;

import com.fastwire.carriere.common.Messages;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.common.util.ResourceUtil;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.controller.ControllerProxy;
import com.fastwire.carriere.controller.CrossConnectController;
import com.fastwire.carriere.controller.impl.CrossConnectControllerImpl;
import com.fastwire.carriere.inventory.vo.ConnectivityVo;
import com.fastwire.carriere.inventory.vo.CrossConnectVo;
import com.fastwire.carriere.service.dataconverter.XMLConverter;
import com.fastwire.carriere.service.webui.exception.ServiceException;
import com.fastwire.carriere.user.vo.SessionVo;

// TODO: Auto-generated Javadoc
/**
 * The Class TunnelInventoryServiceImpl.
 */
@WebService(endpointInterface = "com.fastwire.carriere.service.TunnelInventoryService")
public class TunnelInventoryServiceImpl implements TunnelInventoryService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.TunnelInventoryService#getCrossConnectsbyNeId
	 * (java.lang.String)
	 */
	@Override
	public List<CrossConnectVo> getCrossConnectsbyNeId(String neId,
			SessionVo sessionVo) throws ServiceException {

		try {
			CrossConnectController ccController = (CrossConnectController) ResourceUtil
					.getSessionBindClassByName(
							CrossConnectControllerImpl.class.getName(),
							sessionVo, true);
			List<CrossConnectVo> ccVos = ccController
					.getCrossConnectbyNeId(neId);

			CommonLogger.info(this, " CrossConnect size is: " + ccVos.size());

			return ccVos;
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.service.TunnelInventoryService#
	 * getCrossConnectsbySearchCriteria
	 * (com.fastwire.carriere.inventory.vo.CrossConnectVo)
	 */
	@Override
	public List<String> getCrossConnectNamesbySearchCriteria(
			CrossConnectVo searchVo, SessionVo sessionVo)
			throws ServiceException {

		try {
			CrossConnectController ccController = (CrossConnectController) ResourceUtil
					.getSessionBindClassByName(
							CrossConnectControllerImpl.class.getName(),
							sessionVo, true);
			List<String> ccs = ccController.searchCrossConnect(searchVo);

			return ccs;
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.TunnelInventoryService#getE2EGraphbyNativeName
	 * (java.lang.String)
	 */
	@Override
	public String getE2EGraphbyNativeName(String nativeEmsName,
			SessionVo sessionVo) throws ServiceException {

		CommonLogger.info(this, "getE2EGraphbyNativeName, tunnelName "
				+ nativeEmsName);

		try {

			CrossConnectController ccController = (CrossConnectController) ResourceUtil
					.getSessionBindClassByName(
							CrossConnectControllerImpl.class.getName(),
							sessionVo, false);
			List conVos = ccController.stitchTunnel(nativeEmsName);

			// added by Jun on 20120509  ??
			ccController.stitchRad(conVos);
			
			return XMLConverter.serilizeObjectListToXML(conVos);

		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.service.TunnelInventoryService#
	 * getCrossConnectbyNmeAndNE(java.lang.String, java.lang.String)
	 */
	@Override
	public CrossConnectVo getCrossConnectbyNmeAndNE(String emsNativeName,
			String neID, SessionVo sessionVo) throws ServiceException {

		CommonLogger.info(this, "getCrossConnectbyNmeAndNE, emsNativeName: "
				+ emsNativeName + ",neID:" + neID);

		try {

			CrossConnectController ccController = (CrossConnectController) ResourceUtil
					.getSessionBindClassByName(
							CrossConnectControllerImpl.class.getName(),
							sessionVo, true);

			CrossConnectVo ccVo = ccController.getCrossConnectbyNameAndNE(
					emsNativeName, neID);

			return ccVo;
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.service.TunnelInventoryService#
	 * getCrossConnectsbyNativeNameListAndNeId(java.lang.String[],
	 * java.lang.String)
	 */
	@Override
	public List<CrossConnectVo> getCrossConnectsbyNativeNameListAndNeId(
			String[] nativeEmsNames, String neId, SessionVo sessionVo)
			throws ServiceException {

		CommonLogger.info(this,
				"getCrossConnectsbyNativeNameListAndNeId, nativeEmsNames: "
						+ nativeEmsNames + ",neID:" + neId);

		CrossConnectController ccController = (CrossConnectController) ResourceUtil
				.getSessionBindClassByName(
						CrossConnectControllerImpl.class.getName(), sessionVo,
						true);

		try {
			List<CrossConnectVo> ccVos = ccController
					.getCrossConnectsbyNativeNameListAndNeId(nativeEmsNames,
							neId);
			return ccVos;
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.TunnelInventoryService#getE2EGraphbyFDFr
	 * (java.lang.String)
	 */
	@Override
	public String getE2EGraphbyFDFr(String fdfrName, SessionVo sessionVo)
			throws ServiceException {

		CommonLogger.info(this, "getE2EGraphbyFDFr, fdfrName: " + fdfrName);

		try {
			CrossConnectController ccController = (CrossConnectController) ResourceUtil
					.getSessionBindClassByName(
							CrossConnectControllerImpl.class.getName(),
							sessionVo, true);

			List<ConnectivityVo> connectivityVos = ccController
					.getTunnelGraphsbyFDFrName(fdfrName);
			return XMLConverter.serilizeObjectListToXML(connectivityVos);
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	@Override
	public String updateTunnel(CrossConnectVo ccVo, SessionVo sessionVo)
			throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		if (StringUtil.isEmptyString(ccVo.getRemarks())) {
			return Messages.getString("message.common.error.nullempty",
					new String[] { "Remark" });
		}

		if (StringUtil.isEmptyString(ccVo.getNativeEmsName())) {
			return Messages.getString("message.common.error.nullempty",
					new String[] { "FDFr Native EMS Name" });
		}

		try {
			CrossConnectController ccController = (CrossConnectController) ResourceUtil
					.getSessionBindClassByName(
							CrossConnectControllerImpl.class.getName(),
							sessionVo, true);
			ccController.updateTunnel(ccVo);
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

		return msg;

	}

}
